package com.recruit.server.service.impl;

import com.recruit.server.common.HeaderInfo;
import com.recruit.server.dao.ShippingMapper;
import com.recruit.server.entity.pojo.Shipping;
import com.recruit.server.request.shipping.AddShippingRequest;
import com.recruit.server.request.shipping.ListShippingRequest;
import com.recruit.server.request.shipping.UpdateShippingRequest;
import com.recruit.server.response.product.ProductDetailResponse;
import com.recruit.server.response.shipping.AddShippingResponse;
import com.recruit.server.response.shipping.ListShippingResponse;
import com.recruit.server.response.shipping.UpdateShippingResponse;
import com.recruit.server.service.IShippingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ShippingServiceImpl implements IShippingService {

    @Autowired
    private ShippingMapper shippingMapper;

    /**
     * 获取用户收货地址信息列表
     */
    @Override
    public ListShippingResponse list(ListShippingRequest request, HeaderInfo headerInfo) {
        ListShippingResponse response = new ListShippingResponse();
        try {
            if (headerInfo == null) {
                response.setCodeAndMessage(-1, "未登录，请先登录");
                return response;
            }
            List<Shipping> shippingList = shippingMapper.selectByUserId(headerInfo.getUserId());
            response.setShippingList(shippingList);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            response.setCodeAndMessage(-1, e.getMessage());
        }
        return response;
    }

    /**
     * 添加收货人地址信息
     */
    @Override
    public AddShippingResponse add(AddShippingRequest request, HeaderInfo headerInfo) {
        AddShippingResponse response = new AddShippingResponse();
        try {
            Shipping shipping = new Shipping();
            shipping.setUserId(headerInfo.getUserId());
            shipping.setReceiverName(request.getReceiverName());
            shipping.setReceiverPhone(request.getReceiverPhone());
            shipping.setReceiverMobile(request.getReceiverMobile());
            shipping.setReceiverProvince(request.getReceiverProvince());
            shipping.setReceiverCity(request.getReceiverCity());
            shipping.setReceiverDistrict(request.getReceiverDistrict());
            shipping.setReceiverAddress(request.getReceiverAddress());
            shipping.setReceiverZip(request.getReceiverZip());
            shippingMapper.insert(shipping);
            response.setMsg("create receiver info success");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            response.setCodeAndMessage(-1, e.getMessage());
        }
        return response;
    }

    /**
     * 更新用户收货地址信息
     */
    @Override
    public UpdateShippingResponse update(UpdateShippingRequest request, HeaderInfo headerInfo) {
        UpdateShippingResponse response = new UpdateShippingResponse();
        try {
            Shipping shipping = new Shipping();
            shipping.setId(request.getShippingId());
            shipping.setUserId(headerInfo.getUserId());
            shipping.setReceiverName(request.getReceiverName());
            shipping.setReceiverPhone(request.getReceiverPhone());
            shipping.setReceiverMobile(request.getReceiverMobile());
            shipping.setReceiverProvince(request.getReceiverProvince());
            shipping.setReceiverCity(request.getReceiverCity());
            shipping.setReceiverDistrict(request.getReceiverDistrict());
            shipping.setReceiverAddress(request.getReceiverAddress());
            shipping.setReceiverZip(request.getReceiverZip());
            shippingMapper.updateByPrimaryKeySelective(shipping);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            response.setCodeAndMessage(-1, e.getMessage());
        }
        return response;
    }
}
